package leetcode.linkedlist;

/**
 * description: 给定单链表的头节点 head ，请反转链表，并返回反转后的链表的头节点。
 *
 * @author liux1506
 * @date Created on 2022/9/20
 **/
public class ReverseLinkedList {

    public static void main(String[] args) {
        ListNode listNode5 = new ListNode(5);
        ListNode listNode4 = new ListNode(4, listNode5);
        ListNode listNode3 = new ListNode(3, listNode4);
        ListNode listNode2 = new ListNode(2, listNode3);
        ListNode listNode1 = new ListNode(1, listNode2);

        System.out.println(reverse(listNode1));
    }

    public static ListNode reverse(ListNode head) {
        if(null == head) return null;

        ListNode pre = null,cur = head;
        while (cur != null) {
            ListNode temp = cur.next;
            cur.next = pre;
            pre = cur;
            cur = temp;
        }
        return pre;
    }

}

